package com.warehouse.controller;

import com.warehouse.common.Result;
import com.warehouse.service.CustomerService;
import com.warehouse.service.ProductService;
import com.warehouse.service.OrderService;
import com.warehouse.service.StockInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin(origins = "*")
public class DashboardController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private StockInService stockInService;

    @GetMapping("/stats")
    public Result<Map<String, Object>> getDashboardStats() {
        Map<String, Object> stats = new HashMap<>();
        
        try {
            // 获取客户总数
            long customerCount = customerService.getCustomerCount();
            stats.put("customerCount", customerCount);
            
            // 获取商品总数
            long productCount = productService.getProductCount();
            stats.put("productCount", productCount);
            
            // 获取订单总数
            long orderCount = orderService.getOrderCount();
            stats.put("orderCount", orderCount);
            
            // 获取今日入库数量
            long todayStockInCount = stockInService.getTodayStockInCount();
            stats.put("todayStockInCount", todayStockInCount);
            
            return Result.success(stats);
        } catch (Exception e) {
            return Result.error("获取统计数据失败: " + e.getMessage());
        }
    }
} 